package cn.sxt.yxl.question.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.sxt.yxl.common.util.PageInfoUtil;
import cn.sxt.yxl.question.dao.IColumnDao;
import cn.sxt.yxl.question.pojo.Column;
import cn.sxt.yxl.question.service.IQuestionService;

@Service("questionService")
public class QuestionServiceImpl implements IQuestionService{
	
	@Resource
	private IColumnDao columnDao;
	
	@Override
	public Column findOneColumnSercice(Map<String, Object> map) {
			
		return columnDao.findOne(map);
	}
	
	@Override
	public JSONObject saveOneColumnService(Column column) {
		JSONObject resultJSON = new JSONObject();
		int res = columnDao.saveOne(column);
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", column.getId()+"");
			dataJSON.put("effectCount", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		} else {
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneColumnService(Column column) {
		JSONObject resultJSON = new JSONObject();
		int res = columnDao.updateOne(column);
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", column.getId()+"");
			dataJSON.put("effectCount", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		} else {
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;

	}

	@Override
	public JSONObject deleteOneColumnService(Column column) {
		JSONObject resultJSON = new JSONObject();
		int res = columnDao.deleteOne(column);
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", column.getId()+"");
			dataJSON.put("effectCount", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		} else {
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;

	}
	
	@Override
	public List<Column> findColumnListService(PageInfoUtil pageInfoUtil ,Map<String, Object> map) {
		if(pageInfoUtil != null){
			Page<Column> page = PageHelper.startPage(
					pageInfoUtil.getCurrentPage() , pageInfoUtil.getPageSize());
			List<Column> columnList = columnDao.findList(map);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() +""));
			return columnList;
		}
		return columnDao.findList(map);
	}
}
